package com.atguigu.springcloud.service.impl;

import com.atguigu.springcloud.service.IMessageProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * rabbitmq消息推送，注意每个类所属的包别引入错了
 */
@EnableBinding(Source.class) // 定义消息的推送管道
@Slf4j
public class MessageProviderImpl implements IMessageProvider {

    @Resource
    private MessageChannel output; // 消息发送管道

    /**
     * 向rabbitmq发送消息
     * @return
     */
    @Override
    public String send() {
        // 发送一个流水号
        String serial = UUID.randomUUID().toString();
        log.info(serial);
        output.send(MessageBuilder.withPayload(serial).build()); // 消息构建器
        return null;
    }
}
